package com.yami.shop.app.controller;

import com.yami.shop.app.utils.HttpResult;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.bean.model.CashOut;
import com.yami.shop.service.CashOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yami.shop.service.ShopDetailService;
import java.math.BigDecimal;
import org.json.JSONObject;
import com.yami.shop.app.dto.UmsAccount;


/**
 * 店铺申请提现记录
 * */
@Controller
@RequestMapping("/app/appShopCash")
public class AppShopCashController {

    @Autowired
    private CashOutService cashOutService;
    @Autowired
    private ShopDetailService shopDetailService;

    //申请提现分账
    @PostMapping("/cashOut")
    @ResponseBody
    public HttpResult cashOut(Double cashOutMoney,String remark){
        Long shopId = SecurityUtils.getSysUser().getShopId();
        ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(shopId);
        if( new BigDecimal(cashOutMoney).compareTo(new BigDecimal(shopDetail.getCashMoney()))>0){
            return HttpResult.failure("提现金额大于账户余额");
        }
        CashOut cashOut = cashOutService.appCashOut(cashOutMoney,remark,shopId,SecurityUtils.getSysUser().getUserId());
        return HttpResult.success(cashOut);
    }


    //商户信息查询接口 202006
    @PostMapping("/shopAccount")
    @ResponseBody
    public HttpResult shopAccount(){
        String result = cashOutService.shopAccount();
        return HttpResult.success(result);
    }

    //平台分账
    @PostMapping("/separateAccount")
    @ResponseBody
    public HttpResult separateAccount(){
        Boolean isSuccess = cashOutService.separateAccount();
        if(isSuccess){
            return HttpResult.success();
        }else{
            return HttpResult.failure("分账失败");
        }
    }

    //平台商家查看银联账户余额接口
    @PostMapping("umsShopAccount")
    @ResponseBody
    public HttpResult umsShopAccount(){
        Long shopId = SecurityUtils.getSysUser().getShopId();
        ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(shopId);
        String merNo ="zg000000047866";
        String result = cashOutService.umsShopAccount(shopId,merNo);
        System.out.println("店铺账户余额查询接口返回结果  "+result);
        JSONObject json = new JSONObject(result);
        String respCode = json.getString("respCode");
        UmsAccount umsAccount = new UmsAccount();
        if(respCode.equals("99999999")){
            //成功
            umsAccount.setBalAmt(new BigDecimal(json.getString("balAmt")));
            return HttpResult.success(umsAccount);
        }
        return null;
    }



    //收银机App提现审核
    @PostMapping("/cashOutReviewed")
    @ResponseBody
    public HttpResult cashOutReviewed(Long cashId,int status,String reason){
        ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(SecurityUtils.getSysUser().getShopId());
        CashOut cashOut = cashOutService.getById(cashId);
        if(cashOut.getState()==1){
            return HttpResult.failure("提现申请已驳回，请重新提交");
        }else if(cashOut.getState()==0){
            return HttpResult.failure("提现审核已经通过，请不要重复提交");
        }else if(cashOut.getState()==3){
            return HttpResult.failure("提现申请已经到账，请不要重复提交");
        }
        //店铺可提现余额
        /*if(new BigDecimal(cashOut.getCashOutMoney()).setScale(2,BigDecimal.ROUND_HALF_UP).compareTo(new BigDecimal(shopDetail.getCashMoney()).setScale(2,BigDecimal.ROUND_HALF_UP))>0){
            return HttpResult.failure("提现金额大于账户余额");
        }*/
        //提现状态(0:审核通过1:审核失败2:待审核)
        if (status == 1) { //审核驳回操作
           /* cashOut.setState(status);
            cashOut.setApprovalTime(new Date());
            cashOut.setReason(reason);
            cashOutService.saveOrUpdate(cashOut);
            shopDetail.setCashMoney(cashOut.getCashOutMoney()+shopDetail.getCashMoney());
            shopDetailService.saveOrUpdate(shopDetail);*/
            CashOut cashOut1 = cashOutService.cashOutReject(cashId,status,reason);
            return HttpResult.success(cashOut1);
        }
        Boolean isSuccess = cashOutService.cashOutReview(cashId,status,reason,shopDetail.getCardNumber());
        if(isSuccess){
            return HttpResult.success("提现成功");
        }else{
            return HttpResult.failure("提现失败");
        }
    }


}
